//
//  PinterestCollectionViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/6.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "PinterestCollectionViewController.h"
#import "PinterestCollectionViewLayout.h"

@interface PinterestCollectionViewController ()
<PinterestCollectionViewLayoutDelegate>

@property (nonatomic ,strong) NSMutableArray<UIImage *> *imageNameArray;
@end

@implementation PinterestCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    PinterestCollectionViewLayout *flatout = [[PinterestCollectionViewLayout alloc] init];
    flatout.delegate = self;
    return [self initWithCollectionViewLayout:flatout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self loadImageData];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - PinterestCollectionViewLayoutDelegate

-(CGSize)waterFallLayout:(PinterestCollectionViewLayout *)waterFallLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0)
    {
        return CGSizeZero;
    }
    else
    {
        return CGSizeMake(ScreenWidth, 70);
    }
}

- (CGFloat)waterFallLayout:(PinterestCollectionViewLayout *)waterFallLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth
{
    if (indexPath.section == 0)
    {
        return 100;
    }
    
    //计算cell 高度
    return 75 + arc4random() % 100;
    
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageNameArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

- (void)loadImageData
{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableArray *array = [NSMutableArray array];
        for (int i = 1; i < 10; i ++)
        {
            NSString *name = [NSString stringWithFormat:@"collection_%02d",i];
            NSString *path = [kResourceBundle pathForResource:name ofType:@"jpg"];
            
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            [array addObject:image];
        }
        
        [_imageNameArray addObjectsFromArray:array];
        [_imageNameArray addObjectsFromArray:array];
        [_imageNameArray addObjectsFromArray:array];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    });
    
    
    
}


#pragma mark - Getters And Setters

- (NSMutableArray<UIImage *> *)imageNameArray
{
    if (_imageNameArray == nil)
    {
        _imageNameArray = [NSMutableArray array];
    }
    
    return _imageNameArray;
}



@end
